package com.swpu.cbg.cnshop.utils;

import android.content.Context;
import android.util.SparseArray;

import com.google.gson.JsonArray;
import com.google.gson.reflect.TypeToken;
import com.swpu.cbg.cnshop.bean.ShoppingCart;
import com.swpu.cbg.cnshop.bean.Ware;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.Flushable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenboge on 16/2/11.
 */
public class CartProvider {

    private SparseArray<ShoppingCart> mDatas;
    public static String CART_JSON = "catr_json";

    private Context mContext;

    public CartProvider(Context context) {
        //  mDatas = new SparseArray<>(10);
        this.mContext = context;
        mDatas = getArrayFromList(getListFromLocal());
        if (mDatas == null) {
            mDatas = new SparseArray<>(10);
        }
    }

    public void put(ShoppingCart cart) {
        ShoppingCart temp = mDatas.get(Integer.parseInt(String.valueOf(cart.getId())));
        if (temp != null) {
            temp.setCount(temp.getCount() + 1);
        } else {
            temp = cart;
            temp.setCount(1);
        }
        mDatas.put(Integer.parseInt(String.valueOf(temp.getId())), temp);
        SaveToLocal();
    }

    public void put(Ware ware) {
        ShoppingCart cart = new ShoppingCart();
        cart.setName(ware.getName());
        cart.setPrice(ware.getPrice());
        cart.setImgUrl(ware.getImgUrl());
        cart.setId(ware.getId());
        put(cart);
    }


    public void update(ShoppingCart cart) {
        mDatas.put(Integer.parseInt(String.valueOf(cart.getId())), cart);
        SaveToLocal();
    }

    public void delete(ShoppingCart cart) {
        mDatas.delete(Integer.parseInt(String.valueOf(cart.getId())));
        SaveToLocal();
    }

    public List<ShoppingCart> getAll() {

        return getListFromLocal();
    }

    public List<ShoppingCart> parseToList() {
        int size = mDatas.size();
        List<ShoppingCart> list = new ArrayList<>(10);
        for (int i = 0; i < size; i++) {
            list.add(mDatas.valueAt(i));
        }
        return list;
    }

    public List<ShoppingCart> getListFromLocal() {
        String json = SharperenceUtil.getString(mContext, CART_JSON);
        List<ShoppingCart> carts = null;
        if (json != null) {
            carts = JsonUtil.FromJson(json, new TypeToken<List<ShoppingCart>>() {
            }.getType());
        }
        return carts;
    }


    public void SaveToLocal() {
        List<ShoppingCart> data = parseToList();
        SharperenceUtil.putString(mContext, CART_JSON, JsonUtil.toJSON(data));
    }


    private SparseArray<ShoppingCart> getArrayFromList(List<ShoppingCart> data) {
        SparseArray<ShoppingCart> mShoppingCart = null;
        if (data != null) {
            mShoppingCart = new SparseArray<>();
            for (ShoppingCart cart : data) {
                mShoppingCart.put(Integer.parseInt(String.valueOf(cart.getId())), cart);
            }
        }
        return mShoppingCart;
    }

}
